package com.shangjinrong.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import com.shangjinrong.domain.Lend;
import com.shangjinrong.domain.dto.PageDto;
import com.shangjinrong.result.R;
import com.shangjinrong.service.LendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author Naruto
 */


@RestController
@CrossOrigin
@RequestMapping("admin/lend")
public class AdminLendController {

    private static Gson gson = new Gson();

    @Autowired
    private LendService lendService;

    @GetMapping("list")
    public R listAll() {
        List<Lend> lendList = lendService.list();

        return R.ok().message("获取标的成功").data("data", lendList);
    }


    /**
     * lend分页
     *
     * @param pageDtoStr pageDtoJson对象
     * @return
     */
    @GetMapping("page")
    public R page(@RequestParam("pageDto") String pageDtoStr) {


        PageDto pageDto = gson.fromJson(pageDtoStr, PageDto.class);

        Page<Lend> lendPage = new Page<>(pageDto.getPageIndex(), pageDto.getPageSize());

        lendService.findPage(lendPage, pageDto.getSearch());

        return R.ok().data("page", lendPage);
    }

    /**
     * 获取标的信息
     *
     * @param id 标的id
     * @return 返回标的详细
     */
    @GetMapping("show/{id}")
    public R show(@PathVariable Long id) {
        Map<String, Object> lendDetail = lendService.getLendDetail(id);

        return R.ok().message("获取标的详情成功(●'◡'●)(●'◡'●)(●'◡'●)").data("list", lendDetail);
    }


    @PutMapping("makeLoan/{id}")
    public R makeLoan(@PathVariable Long id) {
        lendService.makeLoan(id);
        return R.ok().message("放款成功");
    }

}
